package com.fy.login.internal;

import com.fy.login.*;

import javax.security.auth.callback.*;
import javax.security.auth.login.LoginException;
import java.io.IOException;

/**
 * @author zhufeifei 2022/8/7
 **/

public class SimpleLoginModule extends AbstractLoginModule {


    public SimpleLoginModule() throws LoginException {
        super();
    }

    @Override
    public boolean login() throws LoginException {
        if (callbackHandler == null) throw new LoginException(NON_CALLBACK_HANDLER);
        PasswordCallback passwordCallback = new PasswordCallback("password:", false);
        MetadataCallback<?> metadataCallback = new MetadataCallback<>();

        handlerCallback(callbackHandler, passwordCallback, metadataCallback);

        return checkLogin(metadataCallback.getMetadata(), passwordCallback.getPassword());
    }

    @Override
    public boolean logout() throws LoginException {
        MetadataCallback<?> metadataCallback = new MetadataCallback<>();
        handlerCallback(callbackHandler, metadataCallback);
        handlerLogout(metadataCallback.getMetadata());
        return true;
    }
}
